import Taro from '@tarojs/taro';

import {Command, GAME_STORAGE_INFO} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async status() {
      // 或取当前状态值
      let {
        main: {isLoading},
      } = getData();
      action.commonChange('main.isLoading', !isLoading);
    },

    /**
     * 查询用户信息
     */
    async loadUserInfo(payload) {
      console.log('action.ts loadUserInfo payload', payload);

      // action.commonChange('main.userInfo', {});
      action.commonChange('main.response.loadUser', '');
      action.commonChange('main.status.loadingUser', true);

      let {data, code, message} = await api.customerBaseController.findCustomerBaseInfoForGame({});
      let userInfo: any = data || null;
      if (!userInfo) {
        userInfo = Taro.getStorageSync(GAME_STORAGE_INFO);
      }
      let accountType = '';
      if (userInfo && userInfo.customerName) {
        // 判断是否是会员
        if (!userInfo.isAquamarineMember && !userInfo.isCrystalMember) {
          accountType = '';
        } else if (userInfo.isAquamarineMember && userInfo.isCrystalMember) {
          accountType = 'isAquamarineMember&isCrystalMember';
        } else {
          accountType = userInfo.isAquamarineMember ? 'isAquamarineMember' : 'isCrystalMember';
        }
      }
      userInfo = {
        ...userInfo,
        accountType,
      };

      action.commonChange('main.userInfo', {
        ...userInfo,
      });
      action.commonChange('main.response.loadUser', code);
      action.commonChange('main.status.loadingUser', false);
    },

    /**
     * 查询列表
     * @param payload
     */
    async loadItems(payload) {
      let {
        main: {items, filter},
      } = getData();
      console.log('goldCoinsDetail loadItems payload', payload);

      // 重置查询就清空items
      if (payload.page.pageNum == 0) {
        action.commonChange('main.items', []);
      }
      action.commonChange('main.response.load', '');
      action.commonChange('main.status.loading', true);

      // 编辑参数
      let params = {
        ...payload.page,
        ...payload.filter,
      };

      let {data, code, message} = await api.GoldCoinsController.getGameCoinDetailByPage(params);
      console.log('goldCoinsDetail loadItems data', data);

      let result = data.content || [];

      // 编辑分页参数
      let pageInfo = {
        ...payload.page,
        total: data.total || 0,
      };
      let filterInfo = {
        ...payload.filter,
      };

      if (pageInfo.pageNum > 0) {
        result = items.concat(data.content);
      }
      console.log('action.tsBUGXX  result', result);

      action.commonChange('main.items', result);
      action.commonChange('main.page', {...pageInfo});
      action.commonChange('main.filter', {...filterInfo});
      action.commonChange('main.response.load', code);
      action.commonChange('main.status.loading', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageGoldCoinsDetailPaMain'),
  };
}
